﻿using System;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Soveryn.Commerce.API;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Pages
{
    public partial class BlogPost : PageBaseClass
    {

        protected new void Page_PreInit(object sender, EventArgs e)
        {
            Website = WebsiteData.WebsiteDetail();
            PageID = Website.BlogTemplatePageID.Value;
            checkTitleForRedirect = false;
            base.Page_PreInit(sender, e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack)
                return;

            int blogPostID = Convert.ToInt32(Page.RouteData.Values["BlogPostID"].ToString());

            DataSet postDS = WebsiteData.BlogPostDetails(blogPostID);
            BlogPostProperties post = BlogPostProperties.Create(postDS);
            post.AddCategories(postDS.Tables[1]);

            //do 301 redirect if page title is incorrect
            if (RouteData.Values["Title"] != null)
                if (RouteData.Values["Title"].ToString() != StringUtils.CleanStringForURL(post.BlogPostTitle).ToLower())
                    Response.RedirectPermanent(BlogPostProperties.GetURL(this, post), true);

            TitleLiteral.Text = post.BlogPostTitle;
            ContentLiterl.Text = post.BlogPostContent;
            SummaryLiteral.Text = post.BlogPostSummary;
            AuthorLiteral.Text = post.BlogPostAuthor;
            DateLiteral.Text = post.BlogPostCreateDate.Value.ToString("D");
            BlogImage.ImageUrl = "/SiteImagesCache/large-" + post.BlogPostImage;

            for (int i = 0;i<post.categoryList.Count;i++)
            {
                BlogCategoryProperties category = post.categoryList[i];
                CategoriesLiteral.Text += "<a href='" + BlogCategoryProperties.GetURL(this, category) + "'>" + category.BlogCategoryTitle + "</a>";
                if (i < post.categoryList.Count - 1)
                    CategoriesLiteral.Text += ",";
            }

        }
    }
}